package pe.edu.upc.matrix.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;

import pe.edu.upc.matrix.excepcion.EExcepcionDAO;
import pe.edu.upc.matrix.modelo.MParametroDetalle;
import pe.edu.upc.matrix.util.UConexionBD;

public class DParametroDetalle extends DBase {
	
	public Collection<MParametroDetalle> consultarTablaParametros(int idParaCabe) throws EExcepcionDAO {
		Collection<MParametroDetalle> lstParaDeta = new ArrayList<MParametroDetalle>();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = UConexionBD.obtenerConexion();
			String query = "select PD.ID_PARA_DETA, PD.NO_PARA_DETA, PD.DE_PARA_DETA, PD.AB_PARA_DETA from tb_parametro_detalle PD, tb_parametro_cabecera PC where PD.ID_PARA_CABE = PC.ID_PARA_CABE and PD.ST_PARA_DETA = 1 and PD.ID_PARA_CABE = ? order by PD.NO_PARA_DETA";
			stmt = con.prepareStatement(query);
			stmt.setInt(1, idParaCabe);
			rs = stmt.executeQuery();
			while (rs.next()) {
				MParametroDetalle paraDeta = new MParametroDetalle();
				paraDeta.setIdParaDeta(rs.getInt("PD.ID_PARA_DETA"));
				paraDeta.setNoParaDeta(rs.getString("PD.NO_PARA_DETA"));
				paraDeta.setDeParaDeta(rs.getString("PD.DE_PARA_DETA"));
				paraDeta.setAbParaDeta(rs.getString("PD.AB_PARA_DETA"));
				lstParaDeta.add(paraDeta);
			}

		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new EExcepcionDAO(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return lstParaDeta;
	}
	
	public String consultarDescParametroDetalle(int idParaDeta) throws EExcepcionDAO {
		String deParaDeta = "";
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = UConexionBD.obtenerConexion();
			String query = "select PD.DE_PARA_DETA from tb_parametro_detalle PD where PD.ID_PARA_DETA = ? ";
			stmt = con.prepareStatement(query);
			stmt.setInt(1, idParaDeta);
			rs = stmt.executeQuery();
			while (rs.next()) {
				deParaDeta = rs.getString("PD.DE_PARA_DETA");
			}

		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new EExcepcionDAO(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return deParaDeta;
	}

}
